﻿using lso_2u_Web应用开发.App_Code.MyActionFilters;
using lso_2u_Web应用开发.App_Code.Service;
using lso_2u_Web应用开发.Models;
using lso_2u_Web应用开发.Models.Database;
using lso_2u_Web应用开发.Models.FormData;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;

namespace lso_2u_Web应用开发.Controllers
{
    public class UserController : Controller
    {

        private readonly DressWorkSystemContext _db;
        private SessionHelper sessionHelper;
        private ServiceClient serviceClient;
        private UserService userService;

        public UserController(DressWorkSystemContext dressWorkSystemContext, IHttpContextAccessor httpContextAccessor)
        {
            _db = dressWorkSystemContext;
            sessionHelper = new SessionHelper(httpContextAccessor.HttpContext.Session);
            serviceClient = new ServiceClient(dressWorkSystemContext);
            userService = serviceClient.getUserService();
        }

        [HttpGet]
        public IActionResult Index()
        {
            if (!sessionHelper.IsLogined)
            {
                return RedirectToAction("Login", "User");
            }

            var workCount = _db.Suppliers.Count(x => x.FkBindUser == sessionHelper.UserId) + _db.Factories.Count(x => x.FkBindUser == sessionHelper.UserId);
            ViewBag.ShowTip = (workCount == 0);
            return View();
        }

        // 登录
        [HttpGet]
        public IActionResult Login()
        {
            return View();
        }

        [HttpGet]
        public IActionResult Logout()
        {
            sessionHelper.Clear();
            return RedirectToAction("Login");
        }
        [HttpPost]
        public IActionResult Login(LoginForm loginForm)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Msg = "数据不合法!";
                return View();
            }

            var user = userService.Login(loginForm.PhoneNumber, loginForm.Password);
            if (user == null)
            {
                ViewBag.Msg = "账号或密码有误!";
                return View();
            }

            sessionHelper.SetLogin(user);

            var workCount = _db.Suppliers.Count(x=> x.FkBindUser == sessionHelper.UserId) + _db.Factories.Count(x=> x.FkBindUser == sessionHelper.UserId);
            if (workCount == 0)
            {
            return RedirectToAction("SignupGuide");
        }

            return RedirectToAction("Index");
        }

        // 分享码登录
        [HttpGet]
        public IActionResult LoginByCode()
        {
            return View();
        }

        // 注册
        [HttpGet]
        public IActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Register(RegisterForm registerForm)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.Msg = "数据不合法!";
                return View();
            }

            var captcha = sessionHelper.CaptchaCode;
            if (!captcha.Equals(registerForm.Captcha))
            {
                ViewBag.Msg = "验证码错误!";
                return View();
            }

            var hasUser = _db.Users.Any(x => x.PhoneNumber == registerForm.PhoneNumber);
            if (hasUser)
            {
                ViewBag.Msg = "用户名已存在!";
                return View();
            }

            var regResult = userService.Register(new User()
            {
                Name = registerForm.UserName,
                PhoneNumber = registerForm.PhoneNumber,
                Password = registerForm.Password,
            });

            if (regResult)
            {
                return RedirectToAction("Login");
            }

            ViewBag.Msg = "注册失败!";
            return View();
        }


        // 引导业务注册
        [CheckSessionString(UserSessionValues.USER_POWER_KEY, UserSessionValues.USER_POWER_USER)]
        [HttpGet]
        public IActionResult SignupGuide()
        {
            return View();
        }


        // 个人信息
        [HttpGet]
        public IActionResult UserInfo()
        {
            var user = _db.Users.FirstOrDefault(x=> x.Id == sessionHelper.UserId);
            return View(user);
        }

        // 安全中心
        [HttpGet]
        public IActionResult SecurityCenter()
        {
            return View();
        }
    }
}
